﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dotnet_lab4
{
    class Fraction : IComparable
    {
        private int numerator;
        private int denominator;

        public int Numerator
        {

            get { return numerator; }
            set
            {
                int gcd = GreatestCommonDivisor(value, Denominator);
                numerator = value / gcd;
                denominator = denominator / gcd;

            }
        }

        public int Denominator
        {
            get { return denominator; }
            set
            {
                int gcd = GreatestCommonDivisor(value, Numerator);

                denominator = value / gcd;
                numerator = numerator / gcd;
            }
        }

        public Fraction()
        {
            numerator = 0;
            denominator = 1;
        }

        public static int GreatestCommonDivisor(int a, int b)
        {
            int n = Math.Min(a, b);
            int gcd = 1, i = 1;

            while (i <= n)
            {
                if (a % i == 0 && b % i == 0)
                {
                    gcd = i;
                }
                i++;
            }
            return gcd;
        }

        public static Fraction operator +(Fraction a, Fraction b)
        {

            Fraction newFraction = new Fraction();
            newFraction.Denominator = a.Denominator * b.Denominator;
            newFraction.Numerator = (a.Numerator * b.Denominator) + (b.Numerator * a.Denominator);

            return newFraction;
        }


        public static Fraction operator -(Fraction a, Fraction b)
        {
            Fraction newFraction = new Fraction();
            newFraction.Denominator = a.Denominator * b.Denominator;
            newFraction.Numerator = (a.Numerator * b.Denominator) - (b.Numerator * a.Denominator);

            return newFraction;
        }


        public static Fraction operator *(Fraction a, Fraction b)
        {
            Fraction newFraction = new Fraction();
            newFraction.Numerator = a.Numerator * b.Numerator;
            newFraction.Denominator = a.Denominator * b.Denominator;

            return newFraction;

        }


        public static Fraction operator /(Fraction a, Fraction b)
        {
            Fraction newFraction = new Fraction();
            newFraction.Numerator = a.Numerator * b.Denominator;
            newFraction.Denominator = a.Denominator * b.Numerator;

            return newFraction;

        }

        public static bool operator ==(Fraction a, Fraction b)
        {
            return a.Numerator * b.Denominator == b.Numerator * a.Denominator;
        }

        public static bool operator !=(Fraction a, Fraction b)
        {
            return (!(a == b));
        }
        public static bool operator >(Fraction a, Fraction b)
        {
            return a.Numerator * b.Denominator > b.Numerator * a.Denominator;
        }
        public static bool operator <(Fraction a, Fraction b)
        {
            return a.Numerator * b.Denominator < b.Numerator * a.Denominator;
        }
        public static bool operator <=(Fraction a, Fraction b)
        {
            return (!(a > b)); ;
        }
        public static bool operator >=(Fraction a, Fraction b)
        {
            return (!(a < b));
        }



        public String toString()
        {
            return numerator + "/" + denominator;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
           // if (obj == null) { return 1; }

            Fraction frac = obj as Fraction;

            if (frac != null)
            {
                if (frac < this) { return 1; }
                else if (frac > this) { return -1; }
                else return 0;
            }
            else
                throw new ArgumentException("Object is not a Fraction");

        }
        #endregion


    }
}
